 /*
 * WLANHOST and UI interface definition
 * Copyright (c) 2021-2027, ASR
 *
 * This file defines a interface used by WLANHOST and UI
 */

/* heron wifi mode */
typedef enum
{
  WLAN_AP,           /* Act as an access point, and other station can connect, 10 stations Max */
  WLAN_STA,          /* Act as a station which can connect to an access point */
  WLAN_MONITOR,      /* Act as a monitor which can recv udp packet */
} wlan_type_e;

typedef enum
{
    WLAN_DHCP_DISABLE = 0,  //use static ip address in station mode
    WLAN_DHCP_CLIENT,
    WLAN_DHCP_SERVER,
} wlan_dhcp_mode_e;

/*used for get heron wifi event */
typedef enum
{
    EVENT_STATION_UP = 1,  /*used in station mode,
                            indicate station associated in open mode or 4-way-handshake done in WPA/WPA2*/
    EVENT_STATION_DOWN,    /*used in station mode, indicate station deauthed*/
    EVENT_AP_UP,           /*used in softap mode, indicate softap enabled*/
    EVENT_AP_DOWN,         /*used in softap mode, indicate softap disabled*/
} wlan_wifi_event_e;

/*used for get heron wifi rssi */
typedef struct
{
    int  level;           /* value: 0, 1, 2, 3, 4 */
    int  rssi_signal;     /* dbm */
} wlan_sta_signal_level_t;


/**
 *  @brief  Scan result using normal scan.
 */
typedef struct
{
  int  is_scan_adv;
  char ap_num;       /**< The number of access points found in scanning. */
  struct {
    char        ssid[32+1];            /* ssid max len:32. +1 is for '\0'. when ssidlen is 32  */
    char        ap_power;              /* Signal strength, min:0, max:100. */
    char        bssid[6];              /* The BSSID of an access point. */
    char        channel;               /* The RF frequency, 1-13 */
    char        security;              /* Security type, @ref wlan_security_type_e */
    signed char rssi;                  /* rssi strength, min:-127, max:-20. */
    char        reserve;               /* ap_list 4 byte align */
  } * ap_list;
} wlan_scan_result_t;


typedef enum
{
    WLAN_SECURITY_OPEN,                   //NONE
    WLAN_SECURITY_WEP,                    //WEP
    WLAN_SECURITY_WPA,                    //WPA
    WLAN_SECURITY_WPA2,                   //WPA2
    WLAN_SECURITY_AUTO,                   //WPA or WPA2
    WLAN_SECURITY_MAX,                    //unsupported security
}wlan_security_type_e;


/*WLAN error status*/
typedef enum
{
    WLAN_STA_MODE_USER_CLOSE = 0,            /* connect fail as reason is user close wifi */
    WLAN_STA_MODE_BEACON_LOSS = 1,           /* cannot receive beacon of peer connected AP for a long time */
    WLAN_STA_MODE_AUTH_FAIL,                 /* connect fail during auth */
    WLAN_STA_MODE_ASSOC_FAIL,                /* connect fail during association */
    WLAN_STA_MODE_PASSWORD_ERR,              /* connect fail as password error */
    WLAN_STA_MODE_NO_AP_FOUND,               /* connect fail as cannot find the connecting AP during scan */
    WLAN_STA_MODE_DHCP_FAIL,                 /* connect fail as dhcp fail */
    WLAN_STA_MODE_CONN_RETRY_MAX,            /* connect fail as reach the max connect retry times */
    WLAN_STA_MODE_CONN_REASON_0,             /* connect fail as reason is 0 */
    WLAN_STA_MODE_CONN_TIMEOUT,              /* connect fail as reason is timeout */
    WLAN_STA_MODE_CONN_AP_CHG_PSK,           /* when connected ap and ap change psk */
    WLAN_STA_MODE_CONN_ENCRY_ERR,            /* when connected ap and ap change psk */
    WLAN_STA_MODE_CONN_RESET,                /* connect fail as sta has reseted */
    WLAN_FW_NEED_HW_RESET,                   /* inform UI to close and re-open both WIFI and BT for HW RESET */
} wlan_sta_err_status_e;

/*WIFI READY status*/
typedef enum
{
    WIFI_NOT_READY = 0,               // wifi is not ready
    WIFI_IS_READY = 1,                   // wifi is ready
}wlan_wifi_ready_status_e;

/*SCAN status*/
typedef enum
{
    WLAN_SCAN_INIT = 0,                      /* scan init value is 0 */
    WLAN_SCAN_FINISH_DOWN = 1,               /* scan finished and scan done */
    WLAN_SCAN_OTHER_ERR,                     /* other link err */
    WLAN_SCAN_SCANING_ERR,                   /* is in scaning mode now,do not scan again */
    WLAN_SCAN_MODE_ERR,                      /* scan mode error,need in ap or sta mode */
    WLAN_SCAN_CONNECT_ERR,                   /* when in connect mode ,do not scan */
    WLAN_SCAN_START,                         /* when start scan set  start */
    WLAN_SCAN_DEINIT ,                       /* wifi deinit then set scan deinit */
    WLAN_SCAN_USER_CLOSE,                    /* wifi close because user close wifi */
    WLAN_SCAN_TIME_OUT,                      /* scan time out for 10s */
}wlan_scan_status_e;

/*used in event callback of station mode, indicate softap informatino which is connected*/
typedef struct
{
    char    ssid[32+1];            /* ssid max len:32. +1 is for '\0' when ssidlen is 32  */
    char    pwd[64+1];             /* pwd max len:64. +1 is for '\0' when pwdlen is 64 */
    char    bssid[6];              /* BSSID of the wlan needs to be connected.*/
    char    ssid_len;              /* ssid length*/
    char    pwd_len;               /* password length*/
    char    channel;               /* wifi channel 0-13.*/
    char    security;              /* refer to wlan_security_type_e*/
} wlan_sta_ap_info_adv_t;

/*only used in station mode*/
typedef struct
{
    char    dhcp;             /* no use currently */
    char    macaddr[16];      /* mac address on the target wlan interface, ASCII*/
    char    ip[16];           /* Local IP address on the target wlan interface, ASCII*/
    char    gate[16];         /* Router IP address on the target wlan interface, ASCII */
    char    mask[16];         /* Netmask on the target wlan interface, ASCII*/
    char    dns[16];          /* no use currently , ASCII*/
    char    broadcastip[16];  /* no use currently , ASCII*/
} wlan_sta_ip_stat_t;

/*only used in station mode*/
typedef struct
{
    int     is_connected;          /* The link to ap is established or not, 0: disconnected, 1: connected. */
    int     wifi_strength;         /* Signal strength of the current connected AP */
    char    ssid[32+1];            /* ssid max len:32. +1 is for '\0'. when ssidlen is 32  */
    char    bssid[6];              /* BSSID of the current connected wlan */
    int     channel;               /* Channel of the current connected wlan */
} wlan_sta_link_stat_t;


/*used in open cmd of wlan wifi */
typedef struct
{
    char    wifi_mode;              /* wlan_type_e */
    char    security;               /* security mode, @ref wlan_security_type_e */
    char    ssid_len;				/* ssid length */
    char    key_len;				/* key length */
    char    ssid[32];               /* in station mode, indicate SSID of the wlan needs to be connected.
                                       in softap mode, indicate softap SSID*/
    char    key[64];                /* in station mode, indicate Security key of the wlan needs to be connected,
                                       in softap mode, indicate softap password.(ignored in an open system.) */
    /* not used in current version */
    char    local_ip_addr[16];      /* used in softap mode to config ip for dut */
    char    net_mask[16];           /* used in softap mode to config gateway for dut */
    char    gateway_ip_addr[16];    /* used in softap mode to config netmask for dut */
    char    dns_server_ip_addr[16]; /* no use currently */
    char    dhcp_mode;              /* no use currently */
    char    channel;                /* softap channel in softap mode; connect channel in sta mode*/
    char    mac_addr[6];            /* connect bssid in sta mode*/
    int     interval;               /* softap mode, beacon listen interval */
    int     hide;                   /* softap mode, hidden SSID */
    /* not used in current version */
} wlan_wifi_init_type_t;




/**
 * @brief:    get sta signal and level
 * @output:   signal(dbm) and level(0,1,2,3,4)
 * @return:   0:success;else failed
*/
int wlan_sta_signal_level_get(wlan_sta_signal_level_t *signal_level);


/** @brief    used in station and softap mode, open wifi cmd
 *
 * @input:    refer to heron_wifi_init_type
 *
 * @return    0: on success; else: error occurred
 */
int wlan_wifi_open(wlan_wifi_init_type_t* init_info);


/** @brief    used in station and softap mode, close wifi cmd
 *            it`s also used to interrupt the process for connecting ap, and then call
 *            wlan_wifi_open to recover wifi service
 *
 * @return    0: on success; else: error occurred
 */
int wlan_wifi_close(void);


/** @brief    used in station mode, connect wifi cmd
 *
 * @input:    refer to heron_wifi_init_type
 *
 * @return    0: on success; else: error occurred
 */
int wlan_sta_connect(wlan_wifi_init_type_t* init_info);


/** @brief    used in station mode, disconnect wifi cmd
 *            only disconnect the active ap connection, and then the STATION_DOWN
 *            will be reported in wlan_sta_cb_stat_chg
 *            note: the ongoing scanning process is not be interrupted
 *
 * @input:    refer to heron_wifi_init_type
 *
 * @return    0: on success; else: error occurred
 */
int wlan_sta_disconnect(unsigned char *mac);



/** @brief    used in station mode, passive scan cmd
 *
 * @input:    none
 *
 * @return    0: on success; else: error occurred
 */
int wlan_sta_start_scan(void);


/** @brief    used in station mode, active scan cmd
 *
 * @input:    target ssid to scan
 *
 * @return    0: on success; else: error occurred
 */
int wlan_sta_start_scan_active(const char *ssid);


/** @brief    used in station mode, get link status information
 *
 * @input:    target ssid to scan
 *
 * @output:   target ssid to scan
 *
 * @return    0: on success; else: error occurred
 */
int wlan_sta_get_link_status(wlan_sta_link_stat_t *link_status);


/** @brief    used in station mode, get the associated ap information
 *
 * @input:    none
 *
 * @output:   associated ap info
 *
 * @return    associated ap info
 */
wlan_sta_ap_info_adv_t *wlan_sta_get_associated_apinfo(void);


/** @brief    used in sniffer mode, open sniffer mode
*
*  @return    0: on success; other: error occurred
*/


int wlan_sta_start_monitor(void);


/** @brief    used in sniffer mode, close sniffer mode
*
*  @return    0: on success.
*  @return    0: on success; other: error occurred
*/
int wlan_sta_stop_monitor(void);


/** @brief    used in sniffer mode, set the sniffer channel, should call this cmd after call start_monitor cmd
 *
 * @input:    WIFI channel(1-13)
 *
 * @output:   none
 *
 * @return    0: on success; else: error occurred
 */
int wlan_sta_monitor_set_channel(int channel);


/** @brief    used in station mode or sniffer mode, call this cmd to send a MPDU constructed by user
 *
 * @input:    buf for MPDU head, len for MPDU length
 *
 * @output:   none
 *
 * @return    0: on success; else: error occurred
 */
int wlan_sta_send_mpdu_frame(unsigned char *buf, int len);


/*when use monitor mode, user should register this type of callback function to get the received MPDU*/
typedef void (*monitor_cb_t)(unsigned char *data, int len);

/** @brief    used in sniffer mode, callback function to get received MPDU, should register before start_monitor
 *
 * @param:    fn: refer to monitor_data_cb_t
 *
 *  @return   0: on success; other: error occurred
 */
int wlan_sta_register_monitor_cb(monitor_cb_t fn);

/* call back */
typedef void (*wlan_sta_cb_stat_chg)(wlan_wifi_event_e wlan_event);

/** @brief    used in station and softap mode,
 *            WIFI stack call this cb when status change, refer to lega_wifi_event_e
 *
 * @input:    fn: cb function type
 *
 * @return    0: on success; other: error occurred
 */
int wlan_stat_chg_cb_register(wlan_sta_cb_stat_chg fn);


typedef void (*wlan_cb_scan_compeleted)(wlan_scan_result_t *scan_result);
/** @brief    used in station mode,
 *            WIFI stack call this cb when scan complete
 *
 * @param:    fn: cb function type
 *
 * @return    0: on success; other: error occurred
 */
int wlan_scan_compeleted_cb_register(wlan_cb_scan_compeleted fn);


typedef void (*wlan_sta_cb_associated_ap)(wlan_sta_ap_info_adv_t *ap_info);
/** @brief    used in station mode,
 *            WIFI stack call this cb when associated with an AP, and tell the AP information
 *
 * @param:    fn: cb function type
 *
 * @return    0: on success; other: error occurred
 */
int wlan_sta_associated_ap_cb_register(wlan_sta_cb_associated_ap fn);


typedef void (*wlan_sta_err_stat_handler)(wlan_sta_err_status_e err_info);
/** @brief   user use to register err status callback function,
 *           WIFI stack call this cb when some error occured, refer to lega_wlan_err_status_e
 *
 * @param:   fn: cb function type
 *
 * @return    0: on success; other: error occurred
 */
int wlan_sta_err_stat_cb_register(wlan_sta_err_stat_handler fn);


typedef void (*wlan_sta_cb_rssi)(char rssi_level, int rssi_signal);
/** @brief   user use to register rssi indication callback function,
 *           WIFI stack call this cb when rssi level change(0,1,2,3,4)
 *
 * @param:   fn: cb function type
 *
 * @return    0: on success; other: error occurred
 */
int wlan_sta_rssi_cb_register(wlan_sta_cb_rssi fn);

int wlan_get_err_code(unsigned char mode);

void wlan_set_max_reconnect_scan(unsigned char counter);

/** @brief   inform wlanhost that whether wlan is turned off after wlan_wifi_close
 *           user call this function before wlan_wifi_close
 *
 * @param:   turn_off_wlan: 0: not turn off wlan, 1: turn of wlan
 *
 * @return    void
 */
void wlan_set_close_cause(unsigned char turn_off_wlan); /* redmine 122036 add by jwlu */

#define HERON_CHIP    3
int GetWiFiType(void);

int sdio_init(int sd_slot, int wifi_pwr_en, int wifi_int_host, int wifi_pwr_reset,
              int host_wakeup_wifi, int sys_clk_32k);

int sdio_wifi_open(void);

void sdio_wifi_close(void);

uint8_t GetWifiConfigFlag(void);

void wifi_lcdWakeup(bool onoff);

/* redmine 138603 add by jwlu
   call by FP/watch UI to inform wifi host that sta has got ip address */
void wifi_inform_dhcp_complete(void);
